package com.gao.web.controller;

import com.gao.web.service.HouseService;
import net.sf.json.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/updateHouse")
public class UpdateHouseServlet extends HttpServlet {

    private HouseService houseService;

    @Override
    public void init() throws ServletException {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        houseService = context.getBean(HouseService.class);
    }

    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonString = request.getParameter("d");
        System.out.println(jsonString);
        JSONObject object = JSONObject.fromObject(jsonString);
        int id = Integer.parseInt(object.getString("id"));
        String build_area = object.getString("build_area");
        String inside_area = object.getString("inside_area");
        String house_type = object.getString("house_type");
        String address = object.getString("address");
        String house_type_diagram = object.getString("house_type_diagram");
        String price = object.getString("price");
        String purchase_date = object.getString("purchase_date");
        String post_date = object.getString("post_date");
        int i = houseService.updateHouse(id, build_area, inside_area, house_type, address, house_type_diagram, price, purchase_date, post_date);
    }
}
